home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / CALC.ICN < prev    next >
Text File  |  1992-11-24  |  3KB  |  112 lines

  1. ############################################################################
  2. #
  3. #    File:     calc.icn
  4. #
  5. #    Subject:  Program to simulate desk calculator
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     April 7, 1992
  10. #
  11. ###########################################################################
  12. #
  13. #  This is a simple Polish "desk calculator".  It accepts as values Icon
  14. #  integers, reals, csets, and strings (as they would appear in an Icon
  15. #  program) as well as an empty line for the null value.
  16. #
  17. #  Other lines of input are interpreted as operations. These may be Icon
  18. #  operators, functions, or the commands listed below.
  19. #
  20. #  In the case of operator symbols, such as +, that correspond to both unary
  21. #  and binary operations, the binary one is used.  Thus, the unary operation
  22. #  is not available.
  23. #
  24. #  In case of Icon functions like write() that take an arbitrary number of
  25. #  arguments, one argument is used.
  26. #
  27. #  The commands are:
  28. #
  29. #    clear    remove all values from the calculator's stack
  30. #    dump    write out the contents of the stack
  31. #    quit    exit from the calculator
  32. #
  33. #  Example: the input lines
  34. #
  35. #    "abc"
  36. #    3
  37. #    repl
  38. #    write
  39. #
  40. #  writes abcabcabc and leaves this as the top value on the stack.
  41. #
  42. #  Failure and most errors are detected, but in these cases, arguments are
  43. #  consumed and not restored to the stack.
  44. #
  45. ############################################################################
  46. #
  47. #  Links: ivalue, usage
  48. #
  49. ############################################################################
  50.  
  51. link ivalue, usage
  52.  
  53. global stack
  54.  
  55. procedure main()
  56.    local line
  57.  
  58.    stack := []
  59.  
  60.    while line := read() do
  61.       (value | operation | command)(line) |
  62.          Error("erroneous input ",  image(line))
  63.  
  64. end
  65.  
  66. procedure command(line)
  67.  
  68.    case line of {
  69.       "clear":    stack := []
  70.       "dump":    every write(image(!stack))
  71.       "quit":     exit()
  72.       default:    fail
  73.       }      
  74.  
  75.    return
  76.  
  77. end
  78.  
  79. procedure operation(line)
  80.    local p, n, arglist
  81.  
  82.    if p := proc(line, 2 | 1 | 3) then {    # function or operation?
  83.       n := abs(args(p))
  84.       arglist := stack[-n : *stack + 1] | {
  85.          Error("too few arguments")
  86.          fail
  87.          }
  88.       stack := stack[1 : -n]
  89.       &error := 1            # anticipate possible error
  90.       put(stack, p ! arglist) | {    # invoke
  91.          if &error = 0 then
  92.             Error("error ", &errornumber, " evaluating ", image(line))
  93.          else
  94.             Error("failure evaluating ", image(line))
  95.          stack |||:= arglist        # restore unused arguments
  96.          }
  97.       &error := 0
  98.       return
  99.       }
  100.  
  101.    else fail
  102.  
  103. end
  104.  
  105. procedure value(line)
  106.  
  107.    put(stack,ivalue(line)) | fail
  108.  
  109.    return
  110.  
  111. end
  112.